<template>
    <a-tree-select
         v-model="checkedKeys"
         :dropdownStyle="{maxHeight: '300px'}"
         :placeholder="placeholder"
         :tree-data="treeData"
         :replaceFields="dataFields"
         allow-clear
         :tree-check-strictly="checkStrictly"
         tree-checkable
         tree-default-expand-all
         :max-tag-count="maxTagCount"
         @change="doCheck"
         @focus="doFocus"
    />
</template>
<script>
import TreeCheckJS from './ComboTreeCheck.js'
export default {
  mixins: [TreeCheckJS],
  methods: {
    initCheckedKeys() {
      // console.log('initCheckedKeys: %s', JSON.stringify(this.value))
      if (this.isRows) {
        const checkedKeys = []
        if (this.checkStrictly) {
          for (let row of this.value) {
            checkedKeys.push({
              label: row.name,
              value: row.id
            })
          }
        } else {
          if (this.value) {
            for (let row of this.value) {
              checkedKeys.push(row.id)
            }
          }
        }
        this.checkedKeys = checkedKeys
      } else if (this.isMap) {
        const checkedKeys = []
        if (this.checkStrictly) {
          for (let id in this.value) {
            checkedKeys.push({
              label: this.value[id],
              value: id
            })
          }
        } else {
          if (this.value) {
            for (let id in this.value) {
              checkedKeys.push(id)
            }
          }
        }
        this.checkedKeys = checkedKeys
      } else {
        const checkedKeys = []
        if (this.checkStrictly) {
          for (let id of this.value) {
            const row = window.$findTreeNode(this.treeData, id)
            checkedKeys.push({
              label: row.name,
              value: row.id
            })
          }
        } else {
          if (this.value) {
            for (let id of this.value) {
              checkedKeys.push(id)
            }
          }
        }
        this.checkedKeys = checkedKeys
      }
    },
    getCheckedKeys() {
      let checkedKeys = []
      if (this.checkStrictly) {
        for (let row of this.checkedKeys) {
          if (row.value) {
            checkedKeys.push(row.value)
          }
        }
      } else {
        checkedKeys = this.checkedKeys
      }
      // console.log('doCheck: checkedKeys = %s', JSON.stringify(checkedKeys))
      return checkedKeys
    },
  }
}
</script>
